﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.IdentityModel.Claims;
using Veracruz.IdentityModel;
using System.Web;

namespace Veracruz.Facebook.IdentityModel
{
    public static class ClaimsIdentityExtensionMethods
    {
        public static Session GetSession(this IClaimsIdentity claimsIdentity)
        {
            if (claimsIdentity == null)
            {
                throw new ArgumentNullException("claimsIdentity");
            }
            Claim l_sessionKeyClaim = claimsIdentity.GetClaim(IdentityClaimDefinitions.SessionKeyClaim);
            Claim l_uniqueIdentifierClaim = claimsIdentity.GetClaim(IdentityClaimDefinitions.UniqueIdentifierClaim);
            if (l_sessionKeyClaim == null || l_uniqueIdentifierClaim == null )
            {

                throw new ArgumentException("The claims identity doesn't contain any facebook session information.", "claimsIdentity");   
                
                //HttpContext.Current.Response.Redirect(string.Format("http://{0}:{1}/login.axd?ReturnUrl={2}", HttpContext.Current.Request.Url.Host, HttpContext.Current.Request.Url.Port,HttpUtility.UrlEncode(HttpContext.Current.Request.Url.AbsoluteUri)));
            }
            return new Session(Convert.ToInt32(l_uniqueIdentifierClaim.Value), l_sessionKeyClaim.Value);
        }
    }
}
